﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Repositories;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;

namespace MvcMall.Tasks
{
    public class SysSettingsTask : ISysSettingsTask
    {
        private readonly ISysSettingsRepository _sysSettingsRepository;

        public SysSettingsTask(ISysSettingsRepository sysSettingsRepository)
        {
            this._sysSettingsRepository = sysSettingsRepository;
        }

        public SysSetting GetDefaultSetting()
        {
            return _sysSettingsRepository.QueryAll().FirstOrDefault();
        }

        public SysSetting Save(SysSettingCommand cmd)
        {
            if (cmd.SysSettingId != null && cmd.SysSettingId > 0)
            {
                //更新
                var updateModel = _sysSettingsRepository.Get(Convert.ToInt32(cmd.SysSettingId));
                if (updateModel != null)
                {
                    updateModel.IsOpen = cmd.IsOpen;
                    updateModel.ClosedReason = cmd.ClosedReason;
                    updateModel.DefGoodsImg = cmd.DefGoodsImg;
                    updateModel.DefShopImg = cmd.DefShopImg;
                    updateModel.DefUserImg = cmd.DefUserImg;
                    updateModel.Keywords = cmd.Keywords;
                    updateModel.SearchWords = cmd.SearchWords;
                    return _sysSettingsRepository.Update(updateModel);
                } 
            }

            var insertModel = new SysSetting()
                             {
                                 IsOpen = cmd.IsOpen,
                                 ClosedReason = cmd.ClosedReason,
                                 DefGoodsImg = cmd.DefGoodsImg,
                                 DefShopImg = cmd.DefShopImg,
                                 DefUserImg = cmd.DefUserImg,
                                 Keywords = cmd.Keywords,
                                 SearchWords = cmd.SearchWords

                             };
            return _sysSettingsRepository.Save(insertModel);

        }

    }
}
